<!-- 物流线路 - 表单编辑页面 -->
<template>
   <div class="page_rela_container" >
		<div class="scroll_auto flex_cell">

    <scheme_edit ref="formRouteScheme" :scheme="scheme" :prescription="line.prescription" :routeSection="routeSection"
      :originCountryCode="line.originCountryCode" :destinationCountryCode="line.destinationCountryCode" />
    </div>
    <div class="page_rela_bottom_area">

      <el-button type="primary" @click="submitForm">{{ $tt('label_submit')
      }}</el-button>
      <el-button @click="$tag.tagClose(null, intoPath)">{{ $tt('label_fms_account_config_cancel') }}</el-button>
    </div>


  </div>
</template>
<script>

import scheme_edit from '@/component/tms/scheme_edit'

const default_scheme_info = {
  segmentSort: 0,//	integer 	路由段排序
  carrierId: null,//	long 	承运商id
  carrierName: "",//	string 	承运商名称
  transportSection: "",//	TransportSectionEnum 	运输段（提货: PICK_UP_GOODS 提货, TRUNK_LINE 干线, DISTRIBUTION 配送
  transportModel: "",//	LogisticsProductsTypeEnum 	运输模式（海运、空运、铁运、陆运、国际快递）: SEA_TRANSPORT 海运, SKY_TRANSPORT 空运, LORRY_NAVIGATE 陆运, INTERNATIONAL_EXPRESS 国际快递, IRON_TRANSPORT 铁运
  serviceItemList: "",//	string 	增值服务项（每个服务之间使用“,”分割）
  loadingMode: "BULK_CARGO",//	LoadingModeEnum 	装载方式(散货、整箱，整车): BULK_CARGO 散货, FULL_CONTAINER 整箱, COMPLETE_VEHICLE 整车
  prescription: 0,//	integer 	运输时效
  originPlaceId: null,//	long 	起点地点id
  destinationPlaceId: null,//	long 	终点地点id
}
const default_obj = {
  "schemeName": "",
  "outPortId": "",
  "outPortName": "",
  "entryPortId": "",
  "entryPortName": "",
  "remark": "",
  transportSegmentInfoParamList: []
}

import router_params from '@/mixin/router_params';
export default {
  title: 'label_add_scheme',
  components: { scheme_edit, },
  mixins: [router_params],

  data() {


    return {
      edit: false,

      loading: false,
      scheme: {},
      line: {},
      routeSection: [],
      routeId: null,
      emitKey: null,
    };
  },


  methods: {

    init_data_default(into_params) {

      this.scheme = _.cloneDeep(default_obj)
      this.edit = !!into_params.edit
      this.routeId = into_params.routeId
      this.emitKey = this.$route.query.emitKey
      if (into_params.id) {
        this.loading = true
        this.$ajax({
          url: "/tms-api/base/TmsLogisticsLineRpc/selectBySchemeId.json",
          method: 'post',
          data: { schemeId: into_params.id },
          success: (res) => {
            this.loading = false
            const { createOrUpdateRouteSectionDTOList, tmsLineInfoParam, ...schemeData } = res.data
            this.scheme = Object.assign(_.cloneDeep(default_obj), schemeData)
            this.line = tmsLineInfoParam
            this.routeSection = createOrUpdateRouteSectionDTOList
          }
        })
      } else if (into_params.routeId) {
        this.loading = true
        this.$ajax({
          url: "/tms-api/base/TmsLogisticsLineRpc/selectByRouteId.json",
          method: 'post',
          data: { routeId: into_params.routeId },
          success: (res) => {
            this.loading = false
            const { createOrUpdateRouteSectionDTOList, tmsLineInfoParam, ...schemeData } = res.data
            this.scheme = Object.assign(_.cloneDeep(default_obj), schemeData)
            this.line = tmsLineInfoParam
            this.routeSection = createOrUpdateRouteSectionDTOList
            this.refreshRouteSection(res.data.tmsRouteInfoDTO)
          }
        })
      }
    },
    refreshRouteSection(route) {
      const routeList = this.routeSection//oute.createOrUpdateRouteSectionDTOList
      routeList.forEach((it, i) => it.sort = i + 1)

      let newSegment = []
      for (let i = 0; i < routeList.length - 1; i++) {
        const originPlace = routeList[i]
        const destinationPlace = routeList[i + 1]
        let segmentItem = null
        if (i < this.scheme.transportSegmentInfoParamList.length) {
          segmentItem = this.scheme.transportSegmentInfoParamList[i]
        } else {
          segmentItem = _.cloneDeep(default_scheme_info)
          // segmentItem.transportModel = route.trunkLineMode
        }
        segmentItem.segmentSort = i + 1
        segmentItem.originPlaceId = originPlace.placeId
        segmentItem.destinationPlaceId = destinationPlace.placeId
        newSegment.push(segmentItem)
      }

      this.scheme.transportSegmentInfoParamList = newSegment

      this.refreshRouteSectionTrunk(route)
    },
    refreshRouteSectionTrunk(route) {
      for (let i = 0; i < this.scheme.transportSegmentInfoParamList.length; i++) {
        let item = this.scheme.transportSegmentInfoParamList[i]
        if (i == 0) {
          item.transportModel = "LORRY_NAVIGATE"
          item.transportSection = "PICK_UP_GOODS"
        } else {
          this.changeTransportSection(item.transportSection, item, route)
        }
      }
    },
    changeTransportSection(e, item, route) {
      if (e == "TRUNK_LINE" ) {
        item.transportModel = route.trunkLineMode
      } else if (e == "DISTRIBUTION" || e == "PICK_UP_GOODS") {
        item.transportModel = "LORRY_NAVIGATE"
      } else {
        item.transportModel = null
      }
    },
    submitForm() {
      if (this.$clickFast()) return
      this['$refs']['formRouteScheme'].validate((valid) => {

        if (valid) {
          this.saveRemote()
        } else {
          this.$message.error(this.$tt('form_validate_error'))
          return false;
        }
      });
    },


    saveRemote() {
      let data = _.cloneDeep(this.scheme)
      delete data.id
      data.routeId = this.routeId
      this.loading = true
      this.$ajax({
        url: '/tms-api/base/TmsLogisticsLineRpc/saveSchemeInfo.json',
        method: 'post', type: 'json',
        data,
        success: (responseData) => {
          this.$message.success(this.$tt('tip_submit_success'))

          this.return_emit(this.emitKey, "reload")
          // this.return_page_back()
          this.$tag.tagClose(null, this.intoPath)
        },
        complete: () => {
          this.loading = false
        }
      });
    },

  }
};

</script>
<style></style>
